/*
 * Created on 6 avr. 2007
 */
package fr.mywiki.business.xml;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 * Contains several methods to manipulate XML files.
 * 
 * @author Thomas
 */
public class XmlManager {
	public static String XmlToString(Element elt) {
		XMLOutputter output = new XMLOutputter("\t", true, "ISO-8859-1");
		StringWriter writer = new StringWriter();
		String result = new String();
		try {
			output.output(elt, writer);
			writer.flush();
			writer.close();
			result = writer.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static String XmlToString(Document doc) {
		XMLOutputter output = new XMLOutputter("\t", true, "ISO-8859-1");
		StringWriter writer = new StringWriter();
		String result = new String();
		try {
			output.output(doc, writer);
			writer.flush();
			writer.close();
			result = writer.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static Document stringToXml(String xml) throws JDOMException {
		SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
    return builder.build(new StringReader(xml));
	}
}
